package com.csdn.leon.adapter;

import java.util.ArrayList;
import java.util.List;

import com.csdn.leon.activity.BlogHostActivity;
import com.csdn.leon.activity.WatchBloggerActivity;
import com.csdn.leon.adapter.BlogAdapter.ViewHolder;
import com.csdn.leon.db.DBUtil;
import com.csdn.leon.model.Person;
import com.csdn.leon.util.Constants;
import com.csdn.leon.util.SPUtils;
import com.csdn.leon.view.CircleImageView;
import com.csdn.leon.view.dialog.CustomDialog;
import com.csdn.leon.view.dialog.DialogHelper;
import com.csdn.leon.view.dialog.CustomDialog.DialogListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.csdn.leon.base.R;

import android.content.Context;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WatchBloggerAdapter extends BaseAdapter {

	private List<Person> list;
	private LayoutInflater inflater;
	private Context context;
	private DisplayImageOptions options;

	public WatchBloggerAdapter(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		if (list == null) {
			list = new ArrayList<Person>();
		}
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.csdn)
		.showImageForEmptyUri(R.drawable.csdn)
		.showImageOnFail(R.drawable.csdn).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.build();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PersonHolder holder = null;
		if (convertView == null) {
			holder = new PersonHolder();
			convertView = inflater.inflate(R.layout.watch_bloger_item, null);
			holder.img = (CircleImageView) convertView
					.findViewById(R.id.userimage);
			holder.name = (TextView) convertView.findViewById(R.id.username);
			holder.bn = (Button) convertView.findViewById(R.id.whatch_bn);
			convertView.setTag(holder);
		} else {
			holder = (PersonHolder) convertView.getTag();
		}

		final Person person =list.get(position);
		holder.name.setText(person.getUsername());
		if ((boolean) SPUtils.get(context, Constants.KEY_LOAD_IMAGE,
				true)) {
		ImageLoader.getInstance().displayImage(person.getUserface(), holder.img, options);
		}
		holder.bn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final CustomDialog dialog = DialogHelper.getCustomDialog(context);
				dialog.show();
				dialog.setDialogListener(new DialogListener() {
					
					@Override
					public void isCancel(boolean flag) {
						dialog.hide();
						if(flag){
								for(int i =0 ;i <list.size();i++){
									if(list.get(i).getUsername().equals(person.getUsername())){
										DBUtil.deleteHost(context, person.getUsername());
										list.remove(i);
										notifyDataSetChanged();
									}
								}
							
						}
					}
				});
			}
		});
		return convertView;
	}

	public List<Person> getList() {
		return list;
	}

	public void setList(List<Person> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	private class PersonHolder {
		private ImageView img;
		private TextView name;
		private Button bn;
	}
}
